cap log close
log using ..\output\logfiles\Table_3_tariffs.smcl, replace

********************************************************************************
********************************************************************************   	
use ..\temp\trade_data_h6_covars_h1.dta, clear

**Globals 
global tariff "tf_best_adv"
global hhi "B1"
global ntm "ntm_EF"
global evasion "gap_evasion_imp_customs"
replace $evasion = . if exp_reported==0 

***Interactions

gen tariffXcom = $tariff* commodity_exports
gen ntmXcom = $ntm *commodity_exports

gen tariffXfcom = $tariff *fuel_exports
gen ntmXfcom = $ntm *fuel_exports

gen tariffXfocom = $tariff *food_exports
gen ntmXfocom = $ntm *food_exports

gen tariffXocom = $tariff *ores_metal_exports 
gen ntmXocom = $ntm *ores_metal_exports

gen tariffXcomp = $tariff *compriceindexfixw
gen ntmXcomp = $ntm *compriceindexfixw

gen tariffXcce = $tariff * cce
gen ntmXcce = $ntm * cce

gen hhiXcom = $hhi * commodity_exports
gen hhiXfcom = $hhi  *fuel_exports
gen hhiXfocom = $hhi  *food_exports
gen hhiXocom = $hhi *ores_metal_exports
gen hhiXcomp = $hhi  *compriceindexfixw

**Labels
lab var $tariff "Tariff [0,1]"
lab var $ntm "NTM (=1)"
lab var $evasion "Evasion [-1,1]"
lab var $hhi "HHI"

lab var tariffXcom "Commodity export share x Tariff"
lab var tariffXocom "Ores and metals export share  x Tariff"
lab var tariffXfocom "Food export share x Tariff "
lab var tariffXfcom "Fuel export share x Tariff"
lab var tariffXcomp "Export commodity price index x Tariff"
lab var tariffXcce "Corruption X Tariff"

lab var hhiXcom "Commodity export share x HHI"
lab var hhiXfcom "Fuel export share x HHI"
lab var hhiXfocom "Food export share x HHI "
lab var hhiXocom "Ores and metals export share x  HHI"
lab var hhiXcomp "Export commodity price index x  HHI"

lab var ntmXcom "Commodity export share x NTM"
lab var ntmXfcom "Fuel export share x NTM"
lab var ntmXocom "Ores and metals export share x NTM"
lab var ntmXfocom "Food export share x NTM"

bys cy: egen v_total = total(v)
gen vshare = v/v_total

lab var commodity_exports "Commodity export share [0,1]"
lab var ores_metal_exports "Ores and metals export share [0,1]"
lab var food_exports "Food export share [0,1]"
lab var fuel_exports "Fuel export share [0,1]"
lab var compriceindexfixw "Export commodity price index (1=2012)"

lab var cce "Control of corruption"
lab var rqe "Regulatory quality"
lab var loggdppc "Log GDPppp per capita"
label var gini_i "Gini coefficient (interpolated) [0, 1]"

********************************************************************************
*Counting one observation per year-country
bys y c: gen x=_n

********************************************************************************

*Checking market size and HH
gen B1_ln = ln(B1)
gen v_ln = ln(v)

reghdfe v_ln B1_ln if $evasion != ., absorb(ch6) vce(robust)
sum $tariff, d
sum $ntm, d

*Table for main text
loc run = 1
if `run' == 1{
    
///Want to keep across product variation so don't use ch6 fixed effects
reghdfe B1  $tariff tariffXcom if $evasion != . , absorb(cy yh6) cluster(cy ch6)
sum  B1  if e(sample)==1
loc m = r(mean)
outreg2 using ..\output\table3_final.tex, replace tex(frag) dec(0) rdec(2) label  nocons addtext( "Product-year fixed effects", "Yes", "Country-year fixed effects", "Yes") sortvar(commodity_exports fuel_exports ores_metal_exports food_exports compriceindexfixw  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom  )  addstat(Dependent variable mean, `m')

reghdfe B1  $tariff tariffXfcom tariffXocom  tariffXfocom if $evasion != ., absorb(cy yh6) cluster(cy ch6)
sum  B1  if e(sample)==1
loc m = r(mean)
outreg2 using ..\output\table3_final.tex, append  tex(frag) dec(0) rdec(2) label  nocons addtext("Product-year fixed effects", "Yes", "Country-year fixed effects", "Yes") sortvar(commodity_exports fuel_exports ores_metal_exports food_exports compriceindexfixw  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom  )   addstat(Dependent variable mean, `m')

test _b[tariffXfcom] = _b[tariffXocom]
test _b[tariffXfcom] = _b[tariffXfocom]

reghdfe $evasion  $tariff tariffXfcom tariffXocom  tariffXfocom , absorb(cy yh6) cluster(cy ch6)
sum  $evasion  if e(sample)==1
loc m = r(mean)
lincom _b[tf_best_adv] + _b[tariffXfcom] //For footnote
outreg2 using ..\output\table3_final.tex, append  tex(frag) dec(2)  label  nocons  addtext("Product-year fixed effects", "Yes", "Country-year fixed effects", "Yes") sortvar(commodity_exports fuel_exports ores_metal_exports food_exports compriceindexfixw  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom  )  addstat(Dependent variable mean, `m')


test _b[tariffXfcom] = _b[tariffXocom]
test _b[tariffXfcom] = _b[tariffXfocom]

}


********************************************************************************
*Table S3 
loc run =0
if `run' == 1{
	
reghdfe cce commodity_exports loggdppc rqe gini_i if x==1,  absorb(c y) cluster(cy)
sum  cce if e(sample)==1
loc m = r(mean)
outreg2 using ..\output\table3_appendix.tex, replace  tex(frag)  label  nocons dec(2) addtext( "Country fixed effects", "Yes", "Year fixed effects", "Yes")   sortvar(commodity_exports fuel_exports ores_metal_exports food_exports compriceindexfixw  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom loggdppc gini_i cce rqe )  addstat(Dependent variable mean, `m')

reghdfe cce fuel_exports ores_metal_exports food_exports loggdppc rqe gini_i if x==1,  absorb(c y) cluster(cy)
sum  cce if e(sample)==1
loc m = r(mean)
outreg2 using ..\output\table3_appendix.tex, append  tex(frag)  label  nocons dec(2)  addtext( "Country fixed effects", "Yes", "Year fixed effects", "Yes") sortvar(commodity_exports fuel_exports ores_metal_exports food_exports compriceindexfixw  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom loggdppc gini_i cce rqe )  addstat(Dependent variable mean, `m')

reghdfe cce compriceindexfixw loggdppc rqe gini_i if x==1,  absorb(c y) cluster(cy)
sum  cce if e(sample)==1
loc m = r(mean)
outreg2 using ..\output\table3_appendix.tex, append  label tex(frag)  nocons dec(2) addtext( "Country fixed effects", "Yes", "Year fixed effects", "Yes")  sortvar(compriceindexfixw commodity_exports fuel_exports ores_metal_exports food_exports  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom )  addstat(Dependent variable mean, `m')

}


********************************************************************************
*Table S4
loc run =0
if `run' == 1{
reghdfe $ntm commodity_exports  if $evasion != ., absorb(h6) cluster(cy ch6)
sum  $ntm if e(sample)==1
loc m = r(mean)
outreg2 using ..\output\table3_appendix2.tex, replace  tex(frag) dec(2)  label  nocons  addtext("Product-year fixed effects", "No", "Country-year fixed effects", "No") sortvar(commodity_exports fuel_exports ores_metal_exports food_exports compriceindexfixw  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom  )  addstat(Dependent variable mean, `m')

reghdfe B1 $ntm ntmXcom  if $evasion != ., absorb(cy h6) cluster(cy ch6)
sum  B1 if e(sample)==1
loc m = r(mean)
outreg2 using ..\output\table3_appendix2.tex, append  tex(frag) dec(2)  label  nocons  addtext("Product-year fixed effects", "No", "Country-year fixed effects", "No") sortvar(commodity_exports fuel_exports ores_metal_exports food_exports compriceindexfixw  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom  )  addstat(Dependent variable mean, `m')

reghdfe B1 $ntm ntmXfcom ntmXocom ntmXfocom if $evasion != ., absorb(cy h6) cluster(cy ch6)
sum  B1  if e(sample)==1
loc m = r(mean)
outreg2 using ..\output\table3_appendix2.tex, append  tex(frag) dec(0) rdec(2) label  nocons  addtext("Product-year fixed effects", "No", "Country-year fixed effects", "Yes")  sortvar(commodity_exports fuel_exports ores_metal_exports food_exports compriceindexfixw  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom  )  addstat(Dependent variable mean, `m')

reghdfe $evasion $ntm ntmXfcom ntmXocom  ntmXfocom ,  absorb(cy h6) cluster(cy ch6)
sum  $evasion  if e(sample)==1
loc m = r(mean)
outreg2 using ..\output\table3_appendix2.tex, append  tex(frag) dec(2)  label  nocons  addtext("Product-year fixed effects", "No", "Country-year fixed effects", "Yes") sortvar(commodity_exports fuel_exports ores_metal_exports food_exports compriceindexfixw  $tariff tariffXcomp tariffXcom   tariffXfcom tariffXocom  tariffXfocom   $ntm ntmXcom ntmXfcom ntmXocom  ntmXfocom  )  addstat(Dependent variable mean, `m')

}


log close


